#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : BaseEditor(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=REF_CLSID;
    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    createMenu();
    updateView();
    startCommander();
}

Widget::~Widget()
{
    stopCommander();
    delete ui;
}

void Widget::startCommander()
{
    commander=new Commander;
    connect(this,&Widget::execute,commander,&Commander::execute);
    connect(commander,&Commander::startSuccess,this,&Widget::onStartSuccess);
    connect(commander,&Commander::startFailure,this,&Widget::onError);
    connect(commander,&Commander::readyRead,this,&Widget::onReadyRead);
    connect(&workerThread,&QThread::finished,commander,&QObject::deleteLater);

    commander->moveToThread(&workerThread);
    workerThread.start();
}

void Widget::stopCommander()
{
    workerThread.quit();
    workerThread.wait();
}

void Widget::onStartSuccess()
{
    ui->display->append("目标程序开始执行……");
}

void Widget::onError()
{
    ui->display->append("目标程序无法启动！");
}

void Widget::onReadyRead(const QString &reply)
{
    ui->display->append(reply);
}


void Widget::generateMenu()
{
    menu->addAction("更新",this,&Widget::updateView);
}

void Widget::updateView()
{
    QStringList items=sp_getItemsByCategory("label",table,"shell");
    listModel->setStringList(items);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_F5)
        updateView();
}

void Widget::on_btn_paste_clicked()
{
    QString kw=qApp->clipboard()->text();
    if(kw.isEmpty())
        return;

    ui->kw->setText(kw);
}

void Widget::on_btn_clean_clicked()
{
    if(!ui->kw->text().isEmpty())
        ui->kw->clear();
}

void Widget::on_kw_returnPressed()
{
    QString kw=ui->kw->text();
    if(kw.isEmpty())
    {
        ui->display->append("关键字 不能为空！");
        return;
    }

    ui->kw->clear();
    if(kw.startsWith("{") && kw.endsWith("}"))
    {
        ui->display->append(kw);
        emit execute(QString(REG_QUERY).arg(kw));
    }
    else
    {
        QStringList items;
        QSqlQuery query;
        query.exec(QString("select label,detail from %1 where label like '%%2%'")
                   .arg(table)
                   .arg(kw));
        while (query.next()) {
            QString label=query.value(0).toString();
            QString detail=query.value(1).toString();
            items.append(label + " :\n" + detail + "\n");
        }

        if(items.isEmpty())
            ui->display->append("数据库中无结果！");
        else
            ui->display->append(items.join("\n"));
    }
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString label=index.data().toString();
        QString detail=sp_getItem("detail",table,"label",label).toString();
        if(detail.isEmpty())
            return;

        ui->display->append(label + "\n" + detail);
        QProcess::startDetached(QString(OPEN_ARG).arg(detail));
    }
}

void Widget::on_cmd_returnPressed()
{
    QString cmd=ui->cmd->text();
    if(cmd.isEmpty())
        return;

    ui->cmd->clear();
    QProcess::startDetached(cmd);
}
